/*
 * SpiderpigOS
 * Copyright (C) 2009 - 2010 Davide Gessa
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "vesa.h"
#include "../../../arch/x86/multiboot.h"
#include <driver.h>
#include <klibc/malloc.h>
#include <types.h>
#include <klibc/string.h>


drv_t drv_vesa = 
{
	.name = "vbe\0",
	.author = "Dak",
						
	.probe = vesa_probe,
	.init = vesa_init,
	.free = NULL
};

vesa_info_t *vci;
vesa_mode_info_t *vmi;
extern multiboot_info_t *multiboot;


vesa_mode_info_t *
vesa_get_mode_info()
{
	return vmi;
}


uint32_t
vesa_probe()
{
	if(multiboot->vbe_control_info != 0)
		return 1;
	else
		return 0;
}

void 
vesa_draw_pixel(uint32_t x, uint32_t y, uint32_t cl)
{
	register char *ptmp;
	
	if (x < 0 || x > vmi->width || y < 0 || y > vmi->height) return;
	
	x = (x * (vmi->bits_per_pixel >> 3));
	y = (y * vmi->bytes_per_scan_line);
	
	char *asd = (char *)vmi->phys_base_ptr;
	ptmp = &asd[x+y];
	ptmp[0] = cl & 0xff;
	ptmp[1] = (cl>>8) & 0xff;
	ptmp[2] = (cl>>16) & 0xff;
}



uint32_t 
vesa_get_pixel(uint32_t x, uint32_t y)
{
	register char *ptmp = NULL;
	
	if (x < 0 || x > vmi->width || y < 0 || y > vmi->height) return 0;
	
	x = (x * (vmi->bits_per_pixel >> 3));
	y = (y * vmi->bytes_per_scan_line);
	
	char *asd = (char *)vmi->phys_base_ptr;
	*ptmp = asd[x+y];
	return *ptmp;	
}


uint32_t 
vesa_set_mode(uint32_t x, uint32_t y, uint32_t bit)
{
	return 0;	
}



uint32_t 
vesa_init()
{
	vci = (vesa_info_t *) multiboot->vbe_control_info;
	vmi = (vesa_mode_info_t *) multiboot->vbe_mode_info;

	return 0;
}



